<?php

class m_ruang extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_new_id() {
        $params = array(date("ymd"));
        $sql = "SELECT CAST(MID(ruang_cd, 8,5) AS UNSIGNED) + 1 'last_number'
                FROM ruang
                WHERE MID(ruang_cd, 2,6) = ?
                ORDER BY ruang_cd DESC ";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            
            $strNumber = '';
            for ($index = strlen($result['last_number']); $index < 5; $index++) {
                $strNumber .= '0';
            }
            $strNumber .= $result['last_number'];
            return 'R'.date("ymd").$strNumber;
        } else {
            return 'R'.date("ymd").'00001';
        }
    }
    
    function get_count_all_ruang() {
        $sql = "SELECT COUNT(ruang_cd)'total' FROM ruang";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 
    
     function get_count_all_ruang_search($params) {
        $sql = "SELECT COUNT(a.ruang_cd)'total'
                FROM ruang a JOIN milik b ON a.milik_cd=b.milik_cd
                WHERE a.ruang_name LIKE ? AND b.milik_cd LIKE ? ";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 
    // check 
    function is_exist_ruang($params) {
        $sql = "SELECT * FROM ruang WHERE ruang_nomor = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $query->free_result();
            return true;
        } else {
            return false;
        }
    } 

    // get user profil
    function get_all_ruang() {
        $sql = "SELECT a.* FROM ruang a
                ORDER BY a.ruang_name ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    } 

    function get_all_ruang_pagination($params) {
        $sql = "SELECT a.*, b.milik_name
                FROM ruang a JOIN milik b ON a.milik_cd=b.milik_cd 
                ORDER BY milik_cd, ruang_name ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_ruang_pagination_search($params) {
        $sql = "SELECT a.*, b.milik_name
                FROM ruang a JOIN milik b ON a.milik_cd=b.milik_cd
                WHERE a.ruang_name LIKE ? AND b.milik_cd LIKE ? 
                ORDER BY milik_cd, ruang_name ASC 
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    
    function get_ruang_by_id($params) {
        $sql = "SELECT a.*, b.milik_name
                FROM ruang a JOIN milik b ON a.milik_cd=b.milik_cd 
                WHERE a.ruang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    } 
    
    function get_count_ruang_by_milik_cd($params) {
        $sql = "SELECT COUNT(ruang_cd)'total' FROM ruang WHERE milik_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return 0;
        }
    } 

    // insert
    function insert($params) {
        // update by this date
        $sql = "INSERT INTO ruang(ruang_cd, ruang_nomor, ruang_name, milik_cd, ruang_type, mdb, mdd) VALUES (?, ?, ?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $params);
    }

    // edit
    function edit($params) {
        // update by this date
        $sql = "UPDATE ruang 
                SET ruang_nomor = ?, 
                ruang_name = ?, 
                milik_cd = ?, 
                ruang_type = ?,
                mdb = ?, 
                mdd = NOW()
                WHERE ruang_cd = ?";
        return $this->db->query($sql, $params);
    }

    // delete
    function delete($params) {
        // update by this date
        $sql = "DELETE FROM ruang WHERE ruang_cd = ?";
        return $this->db->query($sql, $params);
    }

} 